package framework.base.adapter;

import z.http.bean.Image;
import z.http.net.ImageLoader;
import z.http.net.ImageLoader.ImageCallback;
import z.utils.LogUtils;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import framework.base.Base;
import framework.base.config.PhoneParameters;
import framework.base.ui.ContextManager;
import framework.utils.StringUtils;

public abstract class BaseAdap extends BaseAdapter implements Base {
	protected Context context;
	protected ImageLoader imageLoader;
	protected int screenWidth;

	public BaseAdap() {
		super();
		this.context = ContextManager.getInstance().getContext();
		imageLoader = new ImageLoader();
		screenWidth = PhoneParameters.screenWidth;
	}

	protected View inflate(int layoutID) {
		return LayoutInflater.from(context).inflate(layoutID, null);
	}

	protected void setText(TextView view, Object obj) {
		view.setText(String.valueOf(obj));
	}

	protected void setText(TextView view, String str) {
		view.setText(str);
	}

	// 打印信息
	@Override
	public void log(String msg) {
		LogUtils.log(msg);
	}

	@Override
	public void log(Object msg) {
		log(String.valueOf(msg));
	}

	@Override
	public void isNull(String name, Object obj) {
		log(name + "==null:" + (obj == null));
	}

	@Override
	public void exception(Exception e) {
		LogUtils.exception(e);
	}

	//
	protected void setIv(final ImageView iV, String url, final int defaultId, final int w, final int h, final int left, final int top, final int right, final int bottom) {
		if (StringUtils.isNotBlank(url)) {
			Drawable loadDrawable = imageLoader.loadDrawable(new Image(url), new ImageCallback() {

				@Override
				public void imageLoaded(Drawable loadDrawable, String imageUrl) {
					setIv(iV, defaultId, loadDrawable, w, h, left, top, right, bottom);
				}
			});
			setIv(iV, defaultId, loadDrawable, w, h, left, top, right, bottom);
		} else {
			setIv(iV, defaultId, w, h, left, top, right, bottom);
		}
	}

	protected void setIv(final ImageView brandIv, final int defaultId, Drawable loadDrawable, final int w, final int h, final int left, final int top, final int right, final int bottom) {
		if (loadDrawable != null) {
			setIv(brandIv, loadDrawable, w, h, left, top, right, bottom);
		} else {
			setIv(brandIv, defaultId, w, h, left, top, right, bottom);
		}
	}

	protected void setIv(final ImageView iv, int id, int w, int h, int left, int top, int right, int bottom) {
		iv.setBackgroundResource(id);
		LayoutParams lp = new LayoutParams(adjustSize(w), adjustSize(h));
		lp.setMargins(adjustSize(left), adjustSize(top), adjustSize(right), adjustSize(bottom));
		iv.setLayoutParams(lp);
	}

	protected void setIv(final ImageView iv, int id, int w, int h) {
		iv.setBackgroundResource(id);
		LayoutParams lp = new LayoutParams(adjustSize(w), adjustSize(h));
		iv.setLayoutParams(lp);
	}

	protected void setIv(final ImageView iv, Drawable drawable, int w, int h, int left, int top, int right, int bottom) {
		iv.setBackgroundDrawable(drawable);
		LayoutParams lp = new LayoutParams(adjustSize(w), adjustSize(h));
		lp.setMargins(adjustSize(left), adjustSize(top), adjustSize(right), adjustSize(bottom));
		iv.setLayoutParams(lp);
	}

	protected int adjustSize(int ori) {
		return ori * screenWidth / PhoneParameters.designWidth;
	}

	protected float adjustTextSize(int ori) {
		return (ori * screenWidth) / (PhoneParameters.designWidth * PhoneParameters.density);
	}

	// 设置界面
	protected LinearLayout findLinearLayoutById(int id, View view) {
		return (LinearLayout) view.findViewById(id);
	}

	protected Button findButtonById(int id, View view) {
		return (Button) view.findViewById(id);
	}

	protected EditText findEditTextById(int id, View view) {
		return (EditText) view.findViewById(id);
	}

	protected ImageView findImageViewById(int id, View view) {
		return (ImageView) view.findViewById(id);
	}

	protected ImageButton findImageButtonById(int id, View view) {
		return (ImageButton) view.findViewById(id);
	}

	protected RelativeLayout findRelativeLayoutById(int id, View view) {
		return (RelativeLayout) view.findViewById(id);
	}

	protected TextView findTextViewById(int id, View view) {
		return (TextView) view.findViewById(id);
	}

	protected CheckBox findCheckBoxById(int id, View view) {
		return (CheckBox) view.findViewById(id);
	}

	protected ProgressBar findProgressBarById(int id, View view) {
		return (ProgressBar) view.findViewById(id);
	}

	protected FrameLayout findFrameLayoutById(int id, View view) {
		return (FrameLayout) view.findViewById(id);
	}

	protected void viewVisible(int id, View itemView) {
		itemView.findViewById(id).setVisibility(View.VISIBLE);
	}

	protected void viewVisible(View view) {
		view.setVisibility(View.VISIBLE);
	}

	protected void viewGone(int id, View itemView) {
		itemView.findViewById(id).setVisibility(View.GONE);
	}

	protected void viewGone(View view) {
		view.setVisibility(View.GONE);
	}

	protected void setLayoutParams(int id, int w, int h, int left, int top, int right, int bottom, View totalView) {
		setLayoutParams(totalView.findViewById(id), w, h, left, top, right, bottom);
	}

	protected void setLayoutParams(View view, int w, int h, int left, int top, int right, int bottom) {
		LayoutParams layoutParams = getLayoutParams(w, h, left, top, right, bottom);
		view.setLayoutParams(layoutParams);
	}

	protected LayoutParams getLayoutParams(int w, int h, int left, int top, int right, int bottom) {
		if (w > 0) {
			w = adjustSize(w);
		}

		if (h > 0) {
			h = adjustSize(h);
		}
		LayoutParams lp = new LayoutParams(w, h);
		lp.setMargins(adjustSize(left), adjustSize(top), adjustSize(right), adjustSize(bottom));
		return lp;
	}

}
